﻿using System.Collections.Generic;

using System.Web.Mvc;
using eCommMongo.Core;
using eCommMongo.Service.FriendShips;
using eCommMongo.Service.Posts;
using eCommMongo.Service.Tags;
using eCommMongo.Web.Models.Posts;

namespace eCommMongo.Web.Controllers
{
    public class PostController : Controller
    {


        private IPostService _postService;
        private IWorkContext _workContext;
        private ITagService _tagService;

        private IFriendShipService _friendShipService;
        private ICommentService _commentService;

        private int pageSize = 5;

        public PostController(IPostService statusService, IWorkContext workContext, IFriendShipService friendShipService, ICommentService commentService, ITagService tagService)
        {
            _postService = statusService;
            _workContext = workContext;
            _friendShipService = friendShipService;
            _commentService = commentService;
            _tagService = tagService;
        }

        public ActionResult Post(string postId)
        {
            var post = _postService.GetPostById(postId);
            var comments = _postService.GetPagedComments(postId, 0, pageSize);
            post.Comments = comments;

            //var statusModel = Mapper.PostMapper.ToPostModel(post);
            ViewBag.Model = post;
            return View("SinglePost");
        }

        public ActionResult PostTaged( [Bind(Prefix = "t")] string tagName, int? pageIndex)
        {
            var tag= _tagService.FindTagByTagName(tagName);
            var posts = _postService.FindPostsByTagId(tag.Id, pageIndex.HasValue ? pageIndex.Value : 0, pageSize);
            var model = new PostTagedModel()
            {
                PostList = posts
            };
            ViewBag.Model = model; 

            return View();
        }

        public ActionResult Show(string customerId, int? pageIndex)
        {
            var isOwner = customerId == _workContext.CurrentCustomer.Id;

            var posts = isOwner ? _postService.HomeTimeLine(customerId, pageIndex.HasValue ? pageIndex.Value : 0, pageSize,null) :
                                    _postService.UserTimeLine(customerId, pageIndex.HasValue ? pageIndex.Value : 0, pageSize);
            var model = new PostListModel()
                {
                    IsFriend =  _friendShipService.IsFriend(_workContext.CurrentCustomer.Id, customerId),
                    IsOwner = isOwner,
                    CustomerId = customerId,
                    //StatusList = Mapper.PostMapper.PrepareStatusListItemModel(statuses)
                    PostList = posts
                };
            ViewBag.Model = model;
            if (isOwner)
            {

                return View();
            }
            else
            {
                return View("ShowOther");
            }
        }

        public ActionResult UserTimeLines(IList<int> customerIds)
        {
            return View();
        }


        //public ActionResult List()
        //{
        //    var statuses = _statusService.HomeTimeLine(_workContext.CurrentCustomer.Id);
        //    var model = new StatusListModel()
        //        {
        //            StatusList = yiCommerce.Mapper.StatusMapper.PrepareStatusListItemModel(statuses)
        //        };
        //    ViewBag.Model = model;
        //    return View();
        //}


    }
}
